package org.yx.service.smfr.view_process_path_operation;

import java.util.ArrayList;
import java.util.List;

import org.dbist.dml.Page;
import org.dbist.dml.Query;
import org.yx.models.SmfrOprDef;
import org.yx.models.SmfrPthOpr;
import org.yx.utils.BaseService;

import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 查询某工艺路线的工序
 *
 * @author zxw
 * @since 2018/03/30
 **/
public class SmfrViewProcessPathOperationService extends BaseService {
    public SmfrViewProcessPathOperationOut viewProcessPathOperation(SmfrViewProcessPathOperationIn in) throws Exception {
        SmfrViewProcessPathOperationOut out = new SmfrViewProcessPathOperationOut();

        if (StrUtil.isBlank(in.getPathCode())) {
            return out;
        }

        Query query = new Query(in.getPageIndex(), in.getPageSize());
        query.addFilter("orgCode", in.getUserOrgCode());
        List<SmfrOprDef> oprDefList = selectList(SmfrOprDef.class, query);
        query.addFilter("status", "N");
        query.addFilter("pathCode", in.getPathCode());
        query.addOrder("seqNum", true);

        Page<SmfrPthOpr> oprPage = selectPage(SmfrPthOpr.class, query);
        List<PathOper> oprList = new ArrayList<PathOper>();
        for (SmfrPthOpr pthOpr : oprPage.getList()) {
            PathOper oper = new PathOper();
            copyProperties(pthOpr, oper);
            for (SmfrOprDef oprDef : oprDefList) {
                if (oprDef.getOper().equals(oper.getOper()) && 'N' == oprDef.getOperStatus()) {
                    oper.setOperDesc(oprDef.getOperDesc());
                    break;
                }
            }
            oprList.add(oper);
        }
        out.setOperList(oprList);
        out.setTotalSize(oprPage.getTotalSize());
        if (oprList.size() > 0) {
            out.setProcessPath(oprList.get(0).getPathCode());
        }

        return out;
    }
}